package com.example.forecast.city_manager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.forecast.DataBase.DatabaseBean;
import com.example.forecast.Juhe.JuheTempBean;
import com.example.forecast.R;
import com.google.gson.Gson;

import java.util.List;

public class CityManagerAdapter extends BaseAdapter {
    Context context;
    List<DatabaseBean>mDatas;

    public CityManagerAdapter(Context context, List<DatabaseBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();   //返回数据长度
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);   //返回指定位置所对应的元素
    }

    @Override
    public long getItemId(int position) {
        return position;    //返回对应的位置
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_manager,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }


        DatabaseBean bean = mDatas.get(position);
        holder.cityTv.setText(bean.getCity());



        JuheTempBean jHTempBean = new Gson().fromJson(bean.getContent(),JuheTempBean.class);
        JuheTempBean.ResultBean JHResult = jHTempBean.getResult();
        JuheTempBean.ResultBean.RealtimeBean JHRealtime = JHResult.getRealtime();
        JuheTempBean.ResultBean.FutureBean JHTodayBean = JHResult.getFuture().get(0);
//        获取今日天气情况
        /*WeatherBean.ResultsBean.WeatherDataBean dataBean = weatherBean.getResults().get(0).getWeather_data().get(0);*/
        holder.conTv.setText("天气："+JHRealtime.getInfo());
        /*String[] split = dataBean.getDate().split("：");
        String todayTemp = split[1].replace(")","");*/
        holder.currentTempTv.setText(JHRealtime.getTemperature()+"℃");
        holder.windTv.setText(JHRealtime.getDirect()+JHRealtime.getPower());
        holder.tempRangeTv.setText(JHTodayBean.getTemperature());
        return convertView;
    }

    class ViewHolder{
        TextView cityTv,conTv,currentTempTv,windTv,tempRangeTv;
        public ViewHolder(View itemView){
            cityTv = itemView.findViewById(R.id.item_city_tv_city);
            conTv = itemView.findViewById(R.id.item_city_tv_condition);
            currentTempTv = itemView.findViewById(R.id.item_city_tv_temp);
            windTv = itemView.findViewById(R.id.item_city_tv_wind);
            tempRangeTv = itemView.findViewById(R.id.item_city_temprange);
        }
    }
}
